package com.jia.coupons.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jia.coupons.bo.SeckillVoucherBo;
import com.jia.coupons.entity.SeckillVoucher;
import com.jia.coupons.entity.Voucher;
import com.jia.coupons.mapper.SeckillVoucherMapper;
import com.jia.coupons.mapper.VoucherMapper;
import com.jia.coupons.service.SeckillVoucherService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * (SeckillVoucher)表服务实现类
 *
 * @author makejava
 * @since 2024-10-05 21:18:11
 */
@Service("seckillVoucherService")
public class SeckillVoucherServiceImpl extends ServiceImpl<SeckillVoucherMapper, SeckillVoucher>
        implements SeckillVoucherService {

    @Resource
    private SeckillVoucherMapper seckillVoucherMapper;

    @Resource
    private VoucherMapper voucherMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public SeckillVoucherBo insert(SeckillVoucherBo seckillVoucherBo) {
        // 优惠券信息
        Voucher voucher = new Voucher();
        BeanUtil.copyProperties(seckillVoucherBo, voucher);
        voucherMapper.insert(voucher);
        // 库存信息
        SeckillVoucher seckillVoucher = new SeckillVoucher();
        BeanUtil.copyProperties(seckillVoucherBo, seckillVoucher);
        seckillVoucher.setVoucherId(voucher.getId());
        seckillVoucherMapper.insert(seckillVoucher);
        return seckillVoucherBo;
    }


}
